package rpg.state
{
	import flash.geom.Rectangle;

	import rpg.Utils.AStar;
	import rpg.Utils.GameUtils;
	import rpg.Utils.Log;
	import rpg.Utils.Val;
	import rpg.avatar.AvatarComponent;
	import rpg.role.Hero;
	import rpg.update.CameraUpdate;

	public class Walk extends StateBase
	{
		/**
		 * 到达地点后,需要实行的函数
		 */
		public var returnFun : Function;
		public var walkArray : Array;
		private var oldLocalX : int;
		private var oldLocalY : int;
		private var walkStep : int;
		private var walkIndex : int;
		private var oldWalkIndex : int;

		public function Walk(hero : Hero , stateMachine : StateComponent)
		{
			super(hero , stateMachine);
			stateID = Val.WALK;
			clear();
		}

		override public function update(delay : uint) : void
		{
			if(oldLocalX != role.targetMapX || oldLocalY != role.targetMapY)
			{
				oldLocalX = role.targetMapX;
				oldLocalY = role.targetMapY;
				var startCol : int = GameUtils.getGridXByPixel(role.mapX);
				var startRow : int = GameUtils.getGridXByPixel(role.mapY);
				var endCol : int = GameUtils.getGridXByPixel(role.targetMapX);
				var endRow : int = GameUtils.getGridYByPixel(role.targetMapY);
				if(AStar.getInstance().isBlock(endCol , endRow))
				{
					changeState(Val.STAND);
					Log.warning("不可行走");
					return;
				}
				var path : Array = AStar.getInstance().find(startCol , startRow , endCol , endRow);
				walkArray = path;
				if(!walkArray)
					return;
				walkArray.shift();
				walkIndex = 0;
				walkStep = walkArray.length;
				oldWalkIndex = -1;
				if(walkStep == 0)
				{
					changeState(Val.STAND);
					return;
				}
				role.state = Val.WALK;
			}
			if(!walkArray || walkIndex >= walkStep)
			{
				changeState(Val.STAND);
				return;
			}
			var targetX : int = GameUtils.getPixelXByGrid(walkArray[walkIndex][0]);
			var targetY : int = GameUtils.getPixelYByGrid(walkArray[walkIndex][1]);
			var dst : int = GameUtils.getDistance(role.mapX , role.mapY , targetX , targetY);
			var speed : Number = role.data.speed * delay;
			var angle : int = GameUtils.getRotation(role.mapX , role.mapY , targetX , targetY);

			if(oldWalkIndex != walkIndex)
			{
				oldWalkIndex = walkIndex;
				if(dst > speed)
				{
					var dir : int = GameUtils.getDirection(angle);
					role.dir = dir;
				}
			}

			var dx : int;
			var dy : int;
			//距离小于速度,达到这格格子末尾
			if(dst <= Math.ceil(speed))
			{
				dx = targetX - role.mapX;
				dy = targetY - role.mapY;
				checkIsHeroCenterPosition(dx , dy);
				if(++walkIndex >= walkStep - 1)
				{
					if(returnFun != null)
						returnFun();
					else
						changeState(Val.STAND);
				}
			}
			else
			{
				dx = GameUtils.cosD(angle) * speed;
				dy = GameUtils.sinD(angle) * speed;
				checkIsHeroCenterPosition(dx , dy);
			}
		}

		private function checkIsHeroCenterPosition(dx : int , dy : int) : void
		{
			role.mapX += dx;
			role.mapY += dy;
			if(CameraUpdate.aimHero == role)
			{
				CameraUpdate.sceneX += dx;
				CameraUpdate.sceneY += dy;
			}
		}

		override public function changeState(id : int) : void
		{
			stateComponent.changeState(id);
		}

		override public function clear() : void
		{
			oldLocalX = -1;
			oldLocalY = -1;
			walkArray = null;
			returnFun = null;
		}
	}
}